/**
 * @file heap_test_snorch.h
 *
 * @brief test for heap_snorch
 *
 */

 #include <cstdlib>
#include <stdlib.h>
#include <stdio.h>
//#include<list>
//#include <map>
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>

#include <windows.h> //For geting Threads availible
#include <tchar.h>
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>

//#include <strsafe.h>


#include "../../api/heap.h"
#include "../../src/snorch/heap_snorch.h"
//#include "../../src/snorch/heap_snorch.cpp"


#include <iostream>

DWORD ThreadProc (LPVOID lpdwThreadParam ); //for threads to create
const int nGlobalCount = 64;

using namespace std;

const long MyConst = 1024*1024*10;


int degry(int n){
int l=1;
/*
for(int i = 0; i < n; i++)
{
l<<=1;
}
*/
l<<=n;
return l;
}

DWORD WINAPI MyThreadFunction( LPVOID lpParam )
{
    //cout << "Thread!!!" << endl;
    heap_snorch* HS = (heap_snorch*)lpParam;

    cout << "alloc" << endl;
    (int*)HS->alloc(100,0);
    return 0;
}





int main() {
    heap_snorch HS((size_t)MyConst, (heap876::alloc_flags)0);
    HS.free_all(0);

    cout << "Basic test" << endl;
    //Alocation, free and free_all tests
    int* memtest = (int*)HS.alloc(4,0);
    *memtest = 100;
    cout << "Testing memory(must be 100) : "<< *memtest<< endl;
    HS.free((void*)memtest);

    int* mem = (int*)HS.alloc(10*sizeof(int),0);


    //void* mem1 = HS.alloc(400,0);
    (int*)HS.alloc(10,0);




    // get_occupancy test
    cout<< "Occupancy: " << HS.get_occupancy()<<endl;
    HS.free_all(0);


    //Syncronisation test
    cout<< endl <<"Thread Test" << endl;
    cout<< "Starting "<< nGlobalCount<< " threads:"<< endl;
    HANDLE  hThreadArray[nGlobalCount];

    try
	{
		for (int i = 0; i < nGlobalCount; i++)
		{
			hThreadArray[i] = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)(MyThreadFunction), &HS, 0, 0);
		}
		WaitForMultipleObjects(nGlobalCount, hThreadArray, TRUE, INFINITE);
	} catch (exception e) {
		cout << e.what();
	}


    HS.free_all(0);
	return 0;
}

